package io.xccit.dao.impl;

import io.xccit.dao.IBookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author Xccit
 * @Date 2023-02-01
 * @Description
 */
@Repository
public class BookDaoImpl implements IBookDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public Integer getPriceByBookID(Integer bookId) {
        String sql = "select price from t_book where id = ?";
        Integer price = jdbcTemplate.queryForObject(sql, Integer.class, bookId);
        return price;
    }

    @Override
    public void updateStock(Integer bookId) {
        String sql = "update t_book set stock = stock - 1 where id = ?";
        jdbcTemplate.update(sql,bookId);
    }

    @Override
    public void updateBalance(Integer readerId, Integer price) {
        String sql = "update t_reader set balance = balance - ? where id = ?";
        jdbcTemplate.update(sql,price,readerId);
    }
}
